package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 后台系统登录
     *
     * @param employee
     * @param httpSession
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession httpSession) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(lqw);
        if (one == null) {
            return R.error("用户名不存在");
        }
        String oldPwd = employee.getPassword();
        String pwd = DigestUtils.md5DigestAsHex(oldPwd.getBytes());
        String dbPwd = one.getPassword();
        if (!dbPwd.equals(pwd)) {
            return R.error("密码错误");
        }

        Integer status = one.getStatus();
        if (status == 0) {
            return R.error("员工已禁用");
        }

        httpSession.setAttribute("loginEmployee", one);
        one.setPassword(null);
        return R.success(one);
    }

    /**
     * 后台系统退出登录
     *
     * @param httpSession
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession httpSession) {
        httpSession.removeAttribute("loginEmployee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R addEmployee(@RequestBody Employee employee) {
        //Employee loginEmployee = (Employee) httpSession.getAttribute("loginEmployee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setCreateUser(loginEmployee.getId());
        //employee.setUpdateUser(loginEmployee.getId());
        boolean save = employeeService.save(employee);
        return save ? R.success(null) : R.error("添加失败");
    }

    /**
     * 员工分页查询功能
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R getByPage(int page, int pageSize, String name) {
        Page byPage = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name).orderByDesc(Employee::getUpdateTime);
        employeeService.page(byPage, lqw);
        return R.success(byPage);
    }

    /**
     * 员工的禁用和启用
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Employee employee) {
        //Employee loginEmployee = (Employee) httpSession.getAttribute("loginEmployee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(loginEmployee.getId());
        boolean result = employeeService.updateById(employee);
        return result ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return employee != null ? R.success(employee) : R.error("没有查询到对应的员工信息");
    }

    
}
